<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Post;
use App\Comment;

//use DB;

class HomeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
//        \Event::fire('access.front', $request->getClientIp());
        // 关系预载入 用来减少 N + 1 查询问题。
        $articles = Post::with('hasOneUser')->latest()->paginate(6);
        //获取查询的SQL语句，用于条件，不能用于get()之类的结果之中
//        dd(DB::table('posts')->select('post_title')->toSql());
//        dd(Post::select('post_title')->toSql());
//        $recent = Post::recent(6)->->isApproved()->get();
        $random = Post::random(6)->isApproved()->get();
        $comments = Comment::recent(6)->get();
        return view('home', compact('articles', 'random', 'comments'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $article = Post::with('hasOneUser')->find($id);
//        $recent = Post::recent(6)->get();
        $random = Post::random(6)->get();
        $comments = Comment::recent(6)->get();
        return view('posts', compact('article', 'random', 'comments'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }
}
